//
//  SingleChoiceView.swift
//  单选
//
//  Created by 千策万划-mini on 2025/5/6.
//

import Foundation

class SingleChoiceView: UIView {
    
    var modelDetail: ModelAuthPersonalItemInfo = ModelAuthPersonalItemInfo()
    var blockChoiceClick:(() -> Void)?
    
    // MARK: - lazy
    lazy var labelTitle: UILabel = {
        let result = UILabel()
        result.font = UIFont.customSemiBoldFont(size: 16)
        result.textColor = UIColor(hex: "#5228D7")
        return result
    }()
    lazy var imageContentBg: UIButton = {
        let result = UIButton()
        result.isUserInteractionEnabled = true
        result.setBackgroundImage(UIImage(named: "icon_login_agree_bg"), for: .normal)
        result.tag = 1
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var tfContent: UITextField = {
        let result = UITextField()
        result.isUserInteractionEnabled = false
        result.font = UIFont.customBoldFont(size: 14)
        result.textColor = COLOR_WHITE()
        result.textAlignment = .left
        result.isHidden = true
        return result
    }()
    lazy var labelContent: OutlineLabel = {
        let result = OutlineLabel()
        return result
    }()
    lazy var imageArrow: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(W(22), W(22))
        result.image = UIImage(named: "icon_arrow_detail")
        return result
    }()
    lazy var pickerView: SingleChoicePickerView = {
        let result = SingleChoicePickerView()
        return result
    }()
    
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        widthHeight = XY(SCREEN_WIDTH - W(35) * 2, 0)
        backgroundColor = .clear
        addSubView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 添加
    func addSubView() {
        addSubview(labelTitle)
        addSubview(imageContentBg)
        imageContentBg.addSubview(tfContent)
        imageContentBg.addSubview(labelContent)
        imageContentBg.addSubview(imageArrow)
        resetView()
    }
    
    // MARK: - 点击事件
    @objc func btnClick(_ sender: UIControl) {
        feedback()
        sender.addClickAnimate()
        switch sender.tag {
        case 1:
            if let block = blockChoiceClick {
                block()
            }else{
                pickerView.resetView(model: self.modelDetail)
                pickerView.show()
            }
            break
        case 2:

            break
        default:
            break
        }
    }
    
    // MARK: - 重置界面
    func resetView(model: ModelAuthPersonalItemInfo = ModelAuthPersonalItemInfo()) {
        modelDetail = model
        pickerView.titleStr = "Select \(model.whose)"
        labelTitle.fitTitle(UnPackStr(model.whose), variable: 0)
        labelTitle.leftCenterY = XY(0, W(50) / 2)
        imageContentBg.widthHeight = XY(width, W(50))
        imageContentBg.centerXTop = XY(width / 2, W(50))

        tfContent.attributedPlaceholder = NSAttributedString(
            string: UnPackStr(model.itself),
            attributes: [
                .foregroundColor: UIColor(hex: "#2D0084"),
            ]
        )
        
        showContent(content: model.pounding)
        
        height = imageContentBg.bottom
    }
    
    func showContent(content: String = "") {
        tfContent.text = content
        labelContent.isHidden = !isStr(content)
        tfContent.isHidden = isStr(content)
        if isStr(content) {
            labelContent.resetView(
                title: content,
                textColor: COLOR_WHITE(),
                font: UIFont.customBoldFont(size: 14),
                shadowColor: UIColor(hex: "#2D0084"),
                outlineColor: UIColor(hex: "#2D0084")
            )
            labelContent.leftCenterY = XY(W(15), imageContentBg.height/2)
        }
        imageArrow.rightCenterY = XY(imageContentBg.width-W(13), imageContentBg.height / 2)
        tfContent.widthHeight = XY(imageArrow.left - W(15) * 2, imageContentBg.height - 15)
        tfContent.leftCenterY = XY(W(15), imageContentBg.height / 2)
    }
}
